﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
using System.Text;

namespace Abrs.Services.Rest
{
    //[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class UploadFileService:IUploadFileService
    {
        public void FileUpload(string fileName, Stream fileStream)
        {
            var extentSplitIndex = fileName.LastIndexOf("_");
            var newFileName = fileName.Remove(extentSplitIndex, 1).Insert(extentSplitIndex, ".");
            var fileToUpload = new FileStream("D:\\FileUpload\\" + newFileName, FileMode.Create);
            var byteArray = new byte[10000];
            int byteRead, totalByteRead = 0;
            do
            {
                byteRead = fileStream.Read(byteArray, 0, byteArray.Length);
                totalByteRead += byteRead;
            } while (byteRead>0);

            fileToUpload.Write(byteArray, 0, byteArray.Length);
            fileToUpload.Close();
            fileToUpload.Dispose();
        }

        public string FileUpload2(string fileName)
        {
            return fileName;
        }
    }
}
